/**
 * create by caijy 2019/11/30
 */
define(["echarts"], function (echarts) {
    var doc = document;
    var app = win.app, self, pageParameters


    app.newModule({

        init: function () {
            self = this;

            self._super({
                _readyBefore: function () {
                    self.data = {
                        list: [],
                        names: [],
                        startDate: '',
                        endDate: '',
                        div1: true,
                        div2: true,
                        hours: [],
                        todayHours: [],
                        counts: [],
                        todayCounts:[]
                    }
                }
            }, win);// 放在后边

        },
        getList: function () {
            self.data.counts=[];
            self.data.todayCounts=[];
            self.data.hours=[];
            self.data.todayHours=[];
            self.data.names=[];
            app.ajax({
                xurl: 'api/wx/bftj/getList',
                loading: true,
                xdata: {
                    startDate: self.data.startDate,
                    endDate: self.data.endDate
                },
                cb: function (res) {
                    if (res.ok) {
                        var data = res.data.data;
                        self.data.list = data;
                        self.data.list.forEach(function (d, index) {
                            self.data.counts.push(d.counts);
                            self.data.todayCounts.push(d.todayCounts);
                            self.data.hours.push(d.hours)
                            self.data.todayHours.push(d.todayHours);
                            self.data.names.push(d.NAME)
                        })
                        self.formChat(1)
                    }
                }
            })
        },
        formChat: function (type) {
            var chartDiv = echarts.init(doc.getElementById('divChart'));

            var option = {
                legend: {
                    data: self.data.names
                },
                grid: {
                    x: 35,
                    x2: 10,
                    y: 30,
                    y2: 25
                },
                // 提示框组件
                tooltip: {
                    trigger: 'axis', // 触发类型。坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用/none什么都不触发
                },
                toolbox: {
                    show: false,
                    feature: {
                        mark: {
                            show: true
                        },
                        dataView: {
                            show: true,
                            readOnly: false
                        },
                        magicType: {
                            show: true,
                            type: ['bar']
                        },
                        restore: {
                            show: true
                        },
                        saveAsImage: {
                            show: true
                        }
                    }
                },
                calculable: false,
                xAxis: [{
                    type: 'category',
                    data: self.data.names
                }],
                yAxis: [{
                    type: 'value',
                    axisLabel: {
                        formatter: function (d) {
                            return d;
                        }
                    },
                    splitArea: {
                        show: true
                    }
                }],
                series: [{
                    name: '当天拜访',
                    type: 'bar',
                    data: type == 1 ? self.data.todayHours : self.data.todayCounts,
                    label: {
                        normal: {
                            formatter: function (a) {
                                return a.data;
                            },
                            show: true,
                        }
                    },
                },
                    {
                        name: '本期拜访',
                        type: 'bar',
                        data: type == 1 ? self.data.hours : self.data.counts,
                        label: {
                            normal: {
                                formatter: function (a) {
                                    return a.data;
                                },
                                show: true,
                            }
                        },
                    },]
            };
            chartDiv.setOption(option);
        },
        _ready: function () {
            var month = new Date().getCurrentMonth();
            self.data.startDate = month[0];//本月开始日期
            self.data.endDate = new Date().Format('yyyy-MM-dd');

            new Vue({
                el: doc.querySelector('.mui-content'),
                data: self.data,
                watch: {
                    'startDate': function (res) {
                        self.data.startDate = res;
                        self.getList();
                    },
                    'endDate': function (res) {
                        self.data.endDate = res;
                        self.getList();
                    }
                },
                methods: {
                    changeChart: function (type, ele) {

                        var check = ele.target.checked ? true : false;
                        if (check)
                            self.formChat(type)
                    },
                    open:function (v) {
                        app.openPage({
                            src: "bb_bftj/info.html",
                            args: {
                                cb: function (res) {

                                },
                                item:v,
                                startDate:self.data.startDate,
                                endDate:self.data.endDate
                            }
                        })
                    }
                }
            });

            self.getList();

        },
        _lazy: function () {
            this._super();

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        }
    });

});

